Back to course home
0% completed
Vote For New Content
Oracle Hard
Integer to English Words (hard)
Solution: Integer to English Words
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Merge K Sorted Lists (hard)
Solution: Merge K Sorted Lists
Max Points on a Line (hard)
Solution: Max Points on a Line
Sudoku Solver (hard)
Solution: Sudoku Solver
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Remove Nth Node From End of List (Medium)
Solution: Remove Nth Node From End of List
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Rotting Oranges (medium)
Problem Statement
You are given an m x n matrix in which each cell can have one of three values:
0representing anempty cell,1representing afresh orange, or2representing arotten orange.
At each minute, any fresh orange becomes rotten if it is 4-directionally adjacent to a rotten orange.
Return the minimum number of minutes that should be passed until all the orange gets rotten. If it is impossible, return -1.
Examples
- Input: grid =
[[2,1,0,0],
[1,1,1,0],
[0,1,1,1],
[0,0,1,2]]
- Expected Output:
3 - Justification: The rotten oranges at (0,0) and (3,3) spread the rot to all adjacent fresh oranges. By day 4, all reachable fresh oranges are spoiled. The progression is as follows:
- Minute 1: Oranges at positions (0, 1), (1, 0), (2, 3), and (3, 2) gets spoiled.
- Minute 2: Oranges at positions (1, 1), and (2, 2) gets spoiled.
- Minute 3: Oranges at positions (2, 1), and (1, 2) gets spoiled.
Example 2:
- Input: grid =
[[2,1,1],
[1,1,0],
[0,1,2]]
- Expected Output:
2 - Justification: The rotten oranges spread the rot to all fresh oranges in 2 minutes, successfully spoiling all of them.
Example 3:
- Input: grid =
[[0,2],
[1,0],
[0,1]]
- Expected Output:
-1 - Justification: In any case, it is not possible that all oranges gets rotten.
Constraints:
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 10
- grid[i][j] is 0, 1, or 2.
Try it yourself
Try solving this question here:
Java
. . . .
Solution: Longest Common Subsequence
Solution: Rotting Oranges
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself